package org.example.mall.admin.controller.manage;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.example.mall.admin.model.entity.manage.SysDict;
import org.example.mall.admin.model.entity.manage.SysDictItem;
import org.example.mall.admin.service.manage.SysDictItemService;
import org.example.mall.admin.service.manage.SysDictService;
import org.example.mall.admin.util.BusOp;
import org.example.mall.admin.util.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author hzq
 * @since 2022-01-13
 */
@Api(tags = "字典表")
@RestController
@RequestMapping("/manage/sysDict")
@AllArgsConstructor
public class SysDictController {

    private final SysDictService sysDictService;
    private final SysDictItemService sysDictItemService;

    @GetMapping("/page")
    @ApiOperation("分页查询字典")
    public R<IPage<SysDict>> getDictPage(Page page, SysDict dict) {
        return R.ofSuccess(sysDictService.page(page, new QueryWrapper<>(dict)));
    }

    @GetMapping("/{dictId}")
    @ApiOperation("查询字典")
    public R<SysDict> getDict(@PathVariable Integer dictId) {
        return R.ofSuccess(sysDictService.getById(dictId));
    }

    @PostMapping
    @ApiOperation("添加字典")
    @PreAuthorize("hasAuthority('dict_add')")
    public R<Boolean> add(@RequestBody @Valid SysDict dict) {
        return R.ofSuccess("添加字典成功", BusOp.mustSucceed(sysDictService.save(dict), "添加字典失败"));
    }

    @PutMapping
    @ApiOperation("修改字典")
    @PreAuthorize("hasAuthority('dict_edit')")
    public R<Boolean> edit(@RequestBody @Valid SysDict dict) {
        return R.ofSuccess("修改字典成功", BusOp.mustSucceed(sysDictService.edit(dict), "修改字典失败"));
    }

    @DeleteMapping("/{dictId}")
    @ApiOperation("删除字典")
    @PreAuthorize("hasAuthority('dict_del')")
    public R<Boolean> del(@PathVariable Integer dictId) {
        return R.ofSuccess("删除字典成功", BusOp.mustSucceed(sysDictService.del(dictId), "删除字典失败"));
    }

    @GetMapping("/item/type/{type}")
    @ApiOperation("根据类型查询字典项")
    public R<List<SysDictItem>> getDictByType(@PathVariable String type) {
        return R.ofSuccess(sysDictItemService.listByType(type));
    }

    @GetMapping("/item/page")
    @ApiOperation("分页查询字典项")
    public R<IPage<SysDictItem>> getDictItemPage(Page page, SysDictItem dictItem) {
        return R.ofSuccess(sysDictItemService.page(page, Wrappers.query(dictItem)));
    }

    @GetMapping("/item/{dictItemId}")
    @ApiOperation("查询字典项")
    public R<SysDictItem> getDictItem(@PathVariable Integer dictItemId) {
        return R.ofSuccess(sysDictItemService.getById(dictItemId));
    }

    @PostMapping("/item")
    @ApiOperation("添加字典项")
    @PreAuthorize("hasAuthority('dict_add')")
    public R<Boolean> addItem(@RequestBody @Valid SysDictItem dictItem) {
        return R.ofSuccess("添加字典项成功", BusOp.mustSucceed(sysDictItemService.save(dictItem), "添加字典项失败"));
    }

    @PutMapping("/item")
    @ApiOperation("修改字典项")
    @PreAuthorize("hasAuthority('dict_edit')")
    public R<Boolean> editItem(@RequestBody @Valid SysDictItem dictItem) {
        return R.ofSuccess("修改字典项成功", BusOp.mustSucceed(sysDictItemService.edit(dictItem), "修改字典项失败"));
    }

    @DeleteMapping("/item/{dictItemId}")
    @ApiOperation("删除字典项")
    @PreAuthorize("hasAuthority('dict_del')")
    public R<Boolean> delItem(@PathVariable Integer dictItemId) {
        return R.ofSuccess("删除字典项成功", BusOp.mustSucceed(sysDictItemService.del(dictItemId), "删除字典项失败"));
    }

}

